<?php

class MyController extends BaseController
{

    public function init()
    {
        parent::init();
        $this->req = $this->getRequest();
        /* Initialize action controller here */
    }

    /**
     * 我的
     */
    public function indexAction()
    {
        $this->view->title="我的";
        $openid = $this->open_id;
        //$openid = 'o5GoWs4CUYpJO_muCz2LlFWx9EmI';
        $article = new \Base\Rest();
        $req = array('openid'=>$openid);
        $rest_res = $article->get('User',false,$req);
        if ($rest_res->isSuccess()) {
            $res = $rest_res->getResponse();
            $user_info = $this->object2array($res);
            $this->view->user_res = $user_info;
        }
    }

    /**
     * 我的购买列表
     */
    public function buyAction()
    {
        $this->view->title="我的购买";
        $uid = $this->user_id;
        $article = new \Base\Rest();
        $req = array('uid'=>$uid,'buyer_uid'=>$uid);
        $rest_res = $article->get('OrderList',false,$req);

        if ($rest_res->isSuccess()) {
            $res = $rest_res->getResponse();
            $order_list = $this->object2array($res);
            $this->view->order_res = $order_list;
        } else {
            // 没有数据
        }

    }

    /**
     * object转数组
     * @param $object
     * @return mixed
     */
    function object2array(&$object) {
        $object =  json_decode( json_encode( $object),true);
        return  $object;
    }
    /**
     * 买家订单列表
     */
    public function orderAction()
    {
        $this->view->title="我的订单";
        $uid = $this->user_id;
        $article = new \Base\Rest();
        $req = $this->getRequest();
        if($req->get('article_id')){
            $article_id = $req->get('article_id');
            $req = array('uid'=>$uid,'seller_uid'=>$uid,'article_id'=>$article_id);
        }elseif (
            $req = array('uid'=>$uid,'seller_uid'=>$uid)
        )
        $req = array('uid'=>$uid,'seller_uid'=>$uid);
        $rest_res = $article->get('OrderList',false,$req);
        if ($rest_res->isSuccess()) {
            $res = $rest_res->getResponse();
            $order_list = $this->object2array($res);
            $this->view->order_res = $order_list;
        }
        // 获取当前用户名称
        $openid = $this->open_id;
        $article = new \Base\Rest();
        $req = array('openid'=>$openid);
        $rest_res = $article->get('User',false,$req);
        if ($rest_res->isSuccess()) {
            $res = $rest_res->getResponse();
            $user_info = $this->object2array($res);
            $this->view->user_res = $user_info;
            return $this->view->user_res;
        }

    }

    /**
     * 提交物流信息
     */
    public function logisticsAction()
    {
        $this->view->title="物流信息";
        $req = $this->getRequest();

        $id = $this->req->getPost('id');
        $logistics_name = $req->get('express');
        $logistics_numbers = $req->get('number');

        $article = new \Base\Rest();
        $data = array(
            'logistics_name' => $logistics_name,
            'logistics_numbers' => $logistics_numbers,
            'id' => $id,
        );
        $post = array(
            'payload' => base64_encode(http_build_query($data))
        );

        $logistics = $article->post('OrderList',$req,$post);
        if ($logistics->isSuccess()) {
            Response::Success($logistics->getResponse());
        } else {
            Response::Error(500,$logistics->getErrorMessage());
        }

    }

    /**
     * 确认收货
     */
    public function receiptAction()
    {
        $this->view->title="确认收货";
        $req = $this->getRequest();

        $id = $this->req->getPost('id');
        $article = new \Base\Rest();
        $data = array(
            'id' => $id,
        );
        $post = array(
            'payload' => base64_encode(http_build_query($data))
        );

        $logistics = $article->put('OrderList',$id,$req,$post);
        if ($logistics->isSuccess()) {
            Response::Success($logistics->getResponse());
        } else {
            Response::Error(500,$logistics->getErrorMessage());
        }

    }

    /**
     * @return mixed
     * 我的收益
     */
    public function moneyAction()
    {
        $this->view->title="我的收益";
        $uid = $this->user_id;
        $article = new \Base\Rest();
        $req = array('uid'=>$uid,'seller_uid'=>$uid,'pay_status'=>'1');
        $rest_res = $article->get('OrderList',false,$req);
        if ($rest_res->isSuccess()) {
            $res = $rest_res->getResponse();
            $order_list = $this->object2array($res);
            $sum = 0;
            foreach ($order_list as $value=>$vvv){
                if(isset($order_list[$value]['total_price'])){
                    $sum += $order_list[$value]['total_price'];
                }
            }
            $this->view->sum = $sum;
            $this->view->order_res = $order_list;
            return $this->view->order_res;
        }
        
    }

    /**
     * @return string
     * 我的发布
     */
    public function publishAction()
    {
        $this->view->title="我的发布";
        $uid = $this->user_id;
        $article = new \Base\Rest();
        $req = array('uid'=>$uid);
        $rest_res = $article->get('PublicList',false,$req);

        if ($rest_res->isSuccess()) {
            $res = $rest_res->getResponse();
            $publish_list = $this->object2array($res);
            $this->view->public_list = $publish_list;
        } else {
            return '';
        }
        
    }

}

